package com.team.abs.controller;


import java.util.Date;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.abs.entity.User;
import com.team.abs.entity.usershow;
import com.team.abs.service.check;
import com.team.abs.service.userservice;
import com.team.abs.service.usershowservice;

import net.sf.json.JSONObject;

@Controller
public class usercontroller {
	@Autowired
	private userservice us;
	@Autowired 
	private usershowservice ushow;
	private check ch;
	@GetMapping("/register")
	public String register(){
		return "register";
	}
	@GetMapping("sign")
	public String sign() {
		return "sign";
	}
	//简易的注册功能 实际直接导入数据库
	@ResponseBody
	@RequestMapping(value="/manInsert",method=RequestMethod.POST)
	public String manInsert(
			@RequestParam(defaultValue="1")String Uid,
			@RequestParam(defaultValue="张三")String Name,
			@RequestParam(defaultValue="0")int Sex,
			@RequestParam(defaultValue="8888888888")String tel,
			@RequestParam(defaultValue="0")int inorLeave) {
		
		User user=new User(Uid,Name,Sex,tel,inorLeave);
		JSONObject result=new JSONObject();
		//简易判断输入身份证和手机格式
		if(check.check(user)&&us.inserUser(user)>0)
		result.put("status", "success");
		else result.put("status", "fail");
	
		return result.toString();
	} 
	//进行签到用端口
	@ResponseBody
	@RequestMapping(value="/InorLeave",method=RequestMethod.POST)
	public String InorLeave(
			@RequestParam(defaultValue="1")String Uid,
			@RequestParam(defaultValue="0")Integer InorLeave,
			@RequestParam(defaultValue="0")String Place) {
		JSONObject result=new JSONObject();
		if(!check.checkUid(Uid)||us.search_by_uid(Uid).isEmpty())
			{result.put("status", "fail");
			return result.toString();}
		Timestamp Times=new Timestamp(System.currentTimeMillis());
		usershow user=new usershow(Uid,InorLeave,Times,Place);
		//System.out.println(InorLeave);
		
		if(ushow.inorleaveschool(user)>0&&ushow.update_in_or_leave_byuid(user)>0)
		result.put("status", "success");
		else result.put("status", "fail");
		
		return result.toString();
	} 
	//后端定时返回在校人数
	@ResponseBody
	@RequestMapping(value="/search_in",method=RequestMethod.POST)
	public String search_in() {
		JSONObject result=new JSONObject();
		 int num=ushow.search_in(0);
		 result.put("status", "success");
		 result.put("num",num);
		return result.toString();
	} 
	//分页和整个数据的显示
	@RequestMapping(value="/manager")
	public String manager(Model model,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "8")Integer pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<usershow> pageInfo =new PageInfo<usershow>(ushow.search_all());
		//这里出现null问题 卸载包后重装问题解决
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		model.addAttribute("isLastPage",pageInfo.isIsLastPage());
		model.addAttribute("totalPages",pageInfo.getPages());
		model.addAttribute("pageInfo",pageInfo.getList());
		model.addAttribute("total",pageInfo.getTotal());
		model.addAttribute("jumpUrl","/manager?pageNum=");
		return "manager";
	}
	//用名字进行筛选 目前只能用地址传递实现 没有解决数据库的恶意注入问题
	@RequestMapping(value="/searchbyname")
	public String searchbyname(
			Model model,
			@RequestParam(defaultValue="张三")String Name,
			@RequestParam(defaultValue="12")String Uid,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "8")Integer pageSize
			) throws UnsupportedEncodingException {
		Name = URLEncoder.encode(Name, "UTF-8");
		System.out.println(Name);
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<usershow> pageInfo =new PageInfo<usershow>(ushow.search_by_name(Name));
		//这里出现null问题 卸载包后重装问题解决
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		model.addAttribute("isLastPage",pageInfo.isIsLastPage());
		model.addAttribute("totalPages",pageInfo.getPages());
		model.addAttribute("pageInfo",pageInfo.getList());
		model.addAttribute("total",pageInfo.getTotal());
		System.out.println(pageInfo.toString());
		model.addAttribute("jumpUrl","/searchbyname?Name="+Name+"&pageNum=");
		return "manager";
	}
	//用身份证来筛选 同理
	@RequestMapping(value="/searchbyuid")
	public String searchbyuid(	Model model,
			@RequestParam(defaultValue="张三")String Name,
			@RequestParam(defaultValue="12")String Uid,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "8")Integer pageSize){
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<usershow> pageInfo =new PageInfo<usershow>(ushow.search_by_uid(Uid));
		//这里出现null问题 卸载包后重装问题解决
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		model.addAttribute("isLastPage",pageInfo.isIsLastPage());
		model.addAttribute("totalPages",pageInfo.getPages());
		model.addAttribute("pageInfo",pageInfo.getList());
		model.addAttribute("total",pageInfo.getTotal());
		model.addAttribute("jumpUrl","/searchbyuid?Uid="+Uid+"&pageNum=");
		return "manager";
	}
	//用在校/出校来进行筛选 前端已经进行转换
	@RequestMapping(value="/in_or_leaveschool")
	public String inschool(Model model,
			@RequestParam(defaultValue="0")Integer inorLeave,
			@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "8")Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<usershow> pageInfo =new PageInfo<usershow>(ushow.search_by_in_or_leave(inorLeave));
		//这里出现null问题 卸载包后重装问题解决
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		model.addAttribute("isLastPage",pageInfo.isIsLastPage());
		model.addAttribute("totalPages",pageInfo.getPages());
		model.addAttribute("pageInfo",pageInfo.getList());
		model.addAttribute("total",pageInfo.getTotal());
		model.addAttribute("jumpUrl","/in_or_leaveschool?inorLeave="+inorLeave+"&pageNum=");
		return "manager";
	}	
	//获取签到者IP 备用端口
	@ResponseBody
	@RequestMapping(value="/getIp",method=RequestMethod.POST)
	public static String getIp(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
		ip = request.getRemoteAddr();
		}
		System.out.println(ip);
		return ip;
		}
}
